#include"stack.h"
void Print(Stack* stack)
{
	assert(stack);
	for (int i = 0; i < stack->top; i++)
	{
		printf("%d ", stack->a[i]);
	}
	printf("\n");
}
void CheckCapacity(Stack* stack)
{
	if (stack->capacity == stack->top)
	{
		int newcapacity = stack->capacity * 2;
		StackDataType* tmp = (StackDataType*)realloc(stack->a,sizeof(StackDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("malloc failed");
			exit(-1);
		}
		stack->a = tmp;
		stack->capacity = newcapacity;
	}
}
void StackInit(Stack* stack)
{
	assert(stack);
	StackDataType* tmp = (StackDataType*)malloc(sizeof(StackDataType) * 4);
	if (tmp == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	stack->a = tmp;
	stack->top = 0;
	stack->capacity = 4;
}
void StackDestroy(Stack* stack)
{
	assert(stack);
	free(stack->a);
	stack->a = NULL;
	stack->capacity = stack->top = 0;
}
void StackPush(Stack* stack, StackDataType x)
{
	assert(stack);
	CheckCapacity(stack);
	stack->a[stack->top] = x;
	stack->top++;
}
void StackPop(Stack* stack)
{
	assert(stack);
	assert(stack->top > 0);
	stack->top--;
}
StackDataType StackTop(Stack* stack)
{
	assert(stack);
	return stack->a[stack->top - 1];
}
bool StackEmpty(Stack* stack)
{
	assert(stack);
	return stack->top == 0;
}
size_t StackSize(Stack* stack)
{
	assert(stack);
	return stack->top;
}